@()(implicit session: Session)
@config.main("Genewise") {

    <div class="body-box">
        <div class="page-box">

            @config.subTitle("Genewise")

            <div class="main-body">

                <form class="registration-form col-sm-12 my-form" id="form">

                    <div class="row">

                        @components.textarea("Protein sequence", "proteinSeq", "proteinSeq", "GetExample1()",placeholder="Paste protein query sequence(s) here.")

                        @components.textarea("DNA sequence", "dnaSeq", "dnaSeq", "GetExample2()",placeholder="Paste DNA query sequence(s) here.")

                        @components.radio("EMBL Feature Table", "embl", " -embl ", Seq((" -embl ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "9", inputBox = "3",detail="Show EMBL feature format with CDS key")
                        @components.radio("Gene Structure", "genes", " -genes ", Seq((" -genes ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "8", inputBox = "5",detail="Show gene structure")
                        @components.radio("Pretty ASCII", "pretty", " -pretty ", Seq((" -pretty ", "ON"), (" ", "OFF")), divBox = "3", labelBox = "6", inputBox = "5",detail="Show pretty ascii output")

                        @components.radio("EMBL Feature For diana", "diana", " -diana ", Seq((" -diana ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "9", inputBox = "3",detail="Show EMBL feature format with misc_feature key (for diana)")
                        @components.radio("Show Parameters", "para", " -para ", Seq((" -para ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "8", inputBox = "5",detail="Show parameters")
                        @components.radio("cDNA", "cdna", " -cdna ", Seq((" -cdna ", "ON"), (" ", "OFF")), divBox = "3", labelBox = "6", inputBox = "5",detail="Show cDNA")

                        @components.radio("Ace File Gene Structure", "ace", " -ace ", Seq((" -ace ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "9", inputBox = "3",detail="Ace file gene structure")
                        @components.radio("Protein Translation", "trans", " -trans ", Seq((" -trans ", "ON"), (" ", "OFF")), divBox = "4", labelBox = "8", inputBox = "5",detail="Show protein translation, breaking at frameshifts")
                        @components.radio("GFF Output", "gff", " -gff ", Seq((" -gff ", "ON"), (" ", "OFF")), divBox = "3", labelBox = "6", inputBox = "5",detail=" Gene Feature Format file")

                        @components.radio("Random (Null) Model", "random", "syn", Seq(("syn", "Synchronous model"), ("flat", "Flat model")), divBox = "4", labelBox = "9", inputBox = "3",detail=" Random Model as synchronous or flat ")
                        @components.radio("Local/Global Mode", "init", "local", Seq(("local", "Local"), ("global", "Global")), divBox = "8", labelBox = "6", inputBox = "6",detail="[default/global/local/wing/endbias] startend policy for the HMM/protein")

                        @components.radio("Algorithm", "alg", "623", Seq(("623", "GeneWise 623"), ("2193", "GeneWise 2193")), divBox = "4", labelBox = "9", inputBox = "3",detail="Algorithm used ")
                        @components.radio("Splice Site", "splice", " ", Seq((" -nosplice_gtag ", "Modelled"), (" ", " GT/AG only")), divBox = "8", labelBox = "6", inputBox = "6",detail="LEGACY only for -splice flat.")


                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-sm-3"></label>
                        <button type="button" class="btn my-btn" style="width: 200px" onclick="Search()">
                            Run</button>
                    </div>
                </form>

                <div id="result" style="display: none;">
                    <hr>

                    <h5>Result:
                        &nbsp;<button class="btn my-btn" onclick="downloadOut()"  ><i class="fa fa-download"></i>&nbsp;Download</button>
                    </h5>
                    <pre id="out" style="max-height: unset"></pre>
                </div>
            </div>
        </div>
    </div>

    <script src="@routes.Assets.versioned("blast/js/FileSaver.min.js")" type="text/javascript"></script>

    <script>

        $(function () {
            formValidation();

        })


        function Search() {
            let form = $("#form");
            let fv = form.data("formValidation");
            fv.validate()
            if (fv.isValid()) {
                let index = MyTable.prototype.runningIcon();
                $.ajax({
                    url: "@routes.ToolsController.toolsRun("genewise")",
                    type: "post",
                    data: $("#form").serialize(),
                    success: function (data) {
                        layer.close(index);
                        if (data.valid === "false") {
                            swal("Error", data.message, "error");
                            $("#result").hide()
                        } else {
                            $("#out").html(data.out);
                            let $result = $("#result")
                            $result.show();
                            let target_top = $result.offset().top ;
                            $("html,body").animate({scrollTop: target_top}, 800);
                        }
                    }
                });
            }
        }

            function downloadOut() {
                const fileName = "out.txt";
                const content = $("#out").text();
                const blob = new Blob([content], {
                    type: "text/plain;charset=utf-8"
                });
                saveAs(blob, fileName)
            }

            function GetExample1() {
                let eg = ">example1\n" +
                        "MGQGTPGGMGKQGGAPGDRKPGGDGDKKDRKFEPPAAPSRVGRKQRKQKGPEAAARLPNV\n" +
                        "VMKVEKAPLESYADIGGLDAQIQEIKEAVELPLTHPELYEDIGIRPPKGVILYGEPGTGK\n" +
                        "KVTHADFKKAKEKVMFKKKEGVPEGLYM ";
                $('#proteinSeq').val(eg);
                $("#form").formValidation("revalidateField", "proteinSeq");
            }

            function GetExample2(){
                let eg = ">example2\n" +
                        "AATATCACCAGTGTCTTATAAGCAATTACACCAATTTTATTGGGGGTGTTCCTCAATAGT\n" +
                        "TCGACGATCATGATGTCATGTCACCGAGGAATGAAAACGGAGCAGATAATTCTCTAATAG\n" +
                        "TGAGACATACTCCAATTCTTTATAACTCTACCACCTTCAATATACCTTTTAGATTTTATA\n" +
                        "AAGCCCAAGATACTTGAAAAGAACAGAAAAACAGAAG";
                $('#dnaSeq').val(eg);
                $("#form").formValidation("revalidateField", "dnaSeq");
            }

            function formValidation() {
                $('#form').formValidation({
                            framework: 'bootstrap',
                            icon: {
                                valid: 'glyphicon glyphicon-ok',
                                invalid: 'glyphicon glyphicon-remove',
                                validating: 'glyphicon glyphicon-refresh'
                            },
                            fields: {
                                proteinSeq: {
                                    validators: {
                                        notEmpty: {
                                            message: 'Protein sequence is required！'
                                        },
                                        regexp: {
                                            regexp: /^>.*\n[a-zA-Z\n]+\s*$/,
                                            message: "Protein sequence is invalid！"
                                        }
                                    }
                                },
                                dnaSeq: {
                                    validators: {
                                        notEmpty: {
                                            message: 'DNA sequence is required！'
                                        },
                                        regexp: {
                                            regexp: /^>.*\n[ATCGNatcgn\n]+\s*$/,
                                            message: "DNA sequence is invalid！"
                                        }
                                    }
                                },
                                geneId: {
                                    validators: {
                                        notEmpty: {
                                            message: 'Gene Id is required！'
                                        },
                                        blank: {
                                            message: "Gene Id is invalid!"
                                        }
                                    }
                                }
                            }
                        }
                );
            }

    </script>
}